<template>
	<view class="scanpage" @click="startScan()">
		<view class="cont">
			<view class="icon">
				<view class="bar" style="top: -2px;left: -2px;border-bottom: 0;border-right: 0;"></view>
				<view class="bar" style="top: -2px;right: -2px;border-bottom: 0;border-left: 0;"></view>
				<view class="bar" style="bottom: -2px;left: -2px;border-top: 0;border-right: 0;"></view>
				<view class="bar" style="bottom: -2px;right: -2px;border-top: 0;border-left: 0;"></view>
				<view class="hr"></view>
			</view>
			<view class="sp">轻触屏幕唤醒相机扫码</view>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { onShow } from "@dcloudio/uni-app";
const pageState = ref(true)

function startScan() {
	uni.scanCode({
		// onlyFromCamera: true, // 只允许通过相机扫码
		scanType: ['qrCode'], // 只扫描二维码
		success: (res) => {
			let url = res.result || ''
			let params = getQueryParams(url)
			if(params.id){
				uni.navigateTo({
					url: '/pagesC/scans/detail?id='+params.id+(params.type?'&type='+params.type:'')
				})
			} else {
				uni.showModal({
					showCancel: false,
					content: "无法识别图中二维码",
					success: (res) => {
						if (res.confirm) {
							uni.switchTab({
								url:'/pages/index/index'
							})
						}
					}
				})
			}
		},
		fail: (err) => {
			if (err.errMsg.includes('cancel')) {
				pageState.value = true
				uni.switchTab({
					url:'/pages/index/index'
				})
			} else {
				uni.showModal({
					showCancel: false,
					content: "无法识别图中二维码",
					success: (res) => {
						if (res.confirm) {
							uni.switchTab({
								url:'/pages/index/index'
							})
						}
					}
				})
			}
		},
		complete: () => {
			pageState.value = false
		}
	});
}

function getQueryParams(url) {
  const params = {}
  const queryString = url.split('?')[1]
  
  if (queryString) {
    queryString.split('&').forEach(pair => {
      const [key, value] = pair.split('=')
      params[key] = decodeURIComponent(value || '')
    })
  }
  
  return params
}

onShow(() => {
	setTimeout(()=>{
		// #ifdef MP-WEIXIN
		if(pageState.value){
			startScan()
		} else {
			pageState.value = true
		}
		// #endif
	}, 500)
})
</script>

<style lang="less">
page {
	background: #000;
}
.scanpage{
	height: 90vh;
	display: flex;
	align-items: center;
	justify-content: center;
	.cont{
		display: none;
		.icon{
			width: 400rpx;
			height: 400rpx;
			border: 1px solid #fff;
			position: relative;
			.bar{
				width: 120rpx;
				height: 120rpx;
				border: 3px solid #78D0E5;
				position: absolute;
			}
			.hr{
				width: 60%;
				height: 1px;
				background: #78D0E5;
				position: absolute;
				top: 50%;
				left: calc(50% - 30%);
				animation: verticalMove 5s linear infinite;
			}
		}
		.sp{
			color: #fff;
			font-size: 30rpx;
			text-align: center;
			margin-top: 50rpx;
		}
	}
}

@keyframes verticalMove {
	0% {
		top: 0;
		opacity: 0;
	}
	30% {
		opacity: 1;
	}
	50% {
		top: 100%;
		opacity: 0;
	}
	80% {
		opacity: 1;
	}
	100% {
		top: 0;
		opacity: 0;
	}
}
</style>